package com.neusoft.a132wxyorderingdish.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.neusoft.a132wxyorderingdish.R;

public abstract class BaseFragment extends Fragment {
    protected int layout_file=0;
    private SharedPreferences sp;
    protected FragmentManager childManager;
    private FragmentTransaction transaction;
    protected Context context;
    abstract void initView();
    abstract void initEvent();
    abstract void initData();
    View view=null;

    public BaseFragment(){
        context = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view=inflater.inflate(getLayout_file(),container,false);
        initView();
        initEvent();
        initData();
        return view;
    }

    public String getUser_id(){
        sp = getActivity().getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        return sp.getString("user_id","");
    }

    public int getLayout_file() {
        return layout_file;
    }

    public void setLayout_file(int layout_file) {
        this.layout_file = layout_file;
    }

    @Override
    public void onCreate(@Nullable Bundle savedIstanceState){
        super.onCreate(savedIstanceState);
        childManager = getChildFragmentManager();
    }

    protected void addFragment(Fragment fragment){
        transaction = childManager.beginTransaction();
        transaction.add(R.id.fragment_container,fragment);
        transaction.commit();
    }
    @Override
    public  void onAttach(Context context){
        super.onAttach(context);
        this.context = context;
    }
    protected void replaceFragment(Fragment fragment){
        transaction = childManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();
    }
    protected void removeFragment(Fragment fragment){
        transaction = childManager.beginTransaction();
        transaction.remove(fragment);
        transaction.commit();
    }

}



















